package acm.蓝桥杯二分前缀;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

//区间前缀和，在值范围不大时可以使用
public class 天上的星星2 {
    static int[][] dp = new int[2001][2001];

    public static void main(String[] args) {
        Reader sc = new Reader();
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            dp[sc.nextInt()][sc.nextInt()] += sc.nextInt();
        }
        for (int i = 1; i < dp.length; i++) {
            dp[i][0] += dp[i - 1][0];
            dp[0][i] += dp[0][i - 1];
        }
        //前缀和
        for (int i = 1; i < 2001; i++) {
            for (int j = 1; j < 2001; j++) {
                dp[i][j] += dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
            }
        }
        int m = sc.nextInt();
        while (m-- > 0) {
            int x1 = sc.nextInt(), y1 = sc.nextInt();
            int x2 = sc.nextInt(), y2 = sc.nextInt();
            int res=dp[x2][y2];
            if (x1>0){  //减去左部分
                res-=dp[x1-1][y2];
            }
            if (y1>0){  //减去右部分
                res-=dp[x2][y1-1];
            }
            if (x1>0&&y1>0){    //加上重合部分
                res+=dp[x1-1][y1-1];
            }
            System.out.println(res);
        }
    }
    static class Reader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");

        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {    //内存中没有
                String nextLine = innerNextLine();
                if (nextLine == null) {     //且没有下一行
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);  //读入一行
            }
            return true;
        }

        public String nextLine() {
            tokenizer = new StringTokenizer("");    //清空内存中的字符
            return innerNextLine();
        }
        public void clear(){
            tokenizer = new StringTokenizer("");
        }

        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble(){
            return Double.parseDouble(next());
        }
//    public BigInteger nextBigInt() {
//        return new BigInteger(next());
//    }
    }

}
